package com.yuexun.dhdoctor.module.mythings.ui;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.base.bean.model.HelpFileBean;
import com.yuexun.dhdoctor.base.bean.model.HelpFileData;
import com.yuexun.dhdoctor.base.bean.model.HelpFileEntity;
import com.yuexun.dhdoctor.base.bean.model.SubmitEntity;
import com.yuexun.dhdoctor.base.model.impl.HelpModel;
import com.yuexun.dhdoctor.base.presenter.impl.HelpPresent;
import com.yuexun.dhdoctor.base.ui.IHelpView;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.Contract.bean.ChildDepartmentsBean;
import com.yuexun.dhdoctor.module.Contract.bean.GroupGroupData;
import com.yuexun.dhdoctor.module.Contract.bean.People;
import com.yuexun.dhdoctor.module.commun.ui.Act_Choice_Member;
import com.yuexun.dhdoctor.module.commun.ui.Act_Department;
import com.yuexun.dhdoctor.module.commun.ui.Act_Group;
import com.yuexun.dhdoctor.module.email.adapter.MailFileAdapter;
import com.yuexun.dhdoctor.module.email.bean.MailFileBean;
import com.yuexun.dhdoctor.module.email.bean.MailFileDataBean;
import com.yuexun.dhdoctor.module.email.bean.UploadBean;
import com.yuexun.dhdoctor.module.email.model.impl.UploadModel;
import com.yuexun.dhdoctor.module.email.presenter.IUploadPresenter;
import com.yuexun.dhdoctor.module.email.presenter.impl.UploadPresenter;
import com.yuexun.dhdoctor.module.email.ui.view.IUploadView;
import com.yuexun.dhdoctor.module.mythings.bean.AnnualLeaveBean;
import com.yuexun.dhdoctor.module.mythings.bean.FlowMemberData;
import com.yuexun.dhdoctor.module.mythings.bean.TypeData;
import com.yuexun.dhdoctor.module.mythings.model.impl.ThingModel;
import com.yuexun.dhdoctor.module.mythings.presenter.IThingAddPresenter;
import com.yuexun.dhdoctor.module.mythings.presenter.impl.ThingAddPresenter;
import com.yuexun.dhdoctor.module.mythings.ui.view.IThingAddView;
import com.yuexun.dhdoctor.util.CommonUtil;
import com.yuexun.dhdoctor.util.GetPathFromUri4kitkat;
import com.yuexun.dhdoctor.widget.ActionSheetDialog;
import com.yuexun.dhdoctor.widget.FixedSwipeListView;
import com.yuexun.dhdoctor.widget.LoadingDialog;
import com.yuexun.dhdoctor.widget.ModelAddViewNew;
import com.yuexun.dhdoctor.widget.ToastDialog;
import com.yuexun.dhdoctor.widget.UploadDialog;


import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2017/3/14.
 */
public class Act_Thing_Add extends BaseKJActivity implements IUploadView, IThingAddView, IHelpView {
    @Bind(R.id.input_et_title_thing)
    EditText inputEtTitleThing;
    @Bind(R.id.et_person)
    TextView etPerson;
    @Bind(R.id.copy_me_et_person)
    TextView copyMeEtPerson;
    @Bind(R.id.flv_attachment)
    FixedSwipeListView flvAttachment;
    @Bind(R.id.input_et_content_thing)
    EditText inputEtContentThing;
    @Bind(R.id.thing_tv_type)
    TextView thingTvType;
    @Bind(R.id.help_attachment)
    FixedSwipeListView helpAttachment;
    @Bind(R.id.model_view)
    LinearLayout modelView;
    @Bind(R.id.select_txt)
    TextView selectTxt;
    @Bind(R.id.departorpeople)
    TextView departorpeople;
    @Bind(R.id.thing_iv_person_choose)
    ImageView personChoose;
    @Bind(R.id.select_depart)
    LinearLayout selectDepart;
    @Bind(R.id.animProgress)
    ProgressBar animProgress;
    @Bind(R.id.cope_me_ll)
    LinearLayout copeMeLl;
    @Bind(R.id.theme_ll_01)
    LinearLayout themeLl01;
    @Bind(R.id.input_et_title_thing_02)
    TextView inputEtTitleThing02;
    @Bind(R.id.btn_custom)
    TextView btnCustom;
    @Bind(R.id.theme_ll_02)
    LinearLayout themeLl02;
    @Bind(R.id.input_et_title_thing_03)
    EditText inputEtTitleThing03;
    @Bind(R.id.theme_ll_03)
    LinearLayout themeLl03;
    @Bind(R.id.input_et_title_thing_04)
    EditText inputEtTitleThing04;
    @Bind(R.id.theme_ll_04)
    LinearLayout themeLl04;

    //选取人员list
    ArrayList<People> usersBeans_origin = new ArrayList<>();
    ArrayList<People> usersBeans_origin_copy = new ArrayList<>(); //抄送
    //选取部门list
    ArrayList<ChildDepartmentsBean> departmentBeans = new ArrayList<>();
    //选取群组list
    ArrayList<GroupGroupData> groupGroupData = new ArrayList<>();


    UploadDialog uploadDialog;
    IUploadPresenter presenter_upload;
    IThingAddPresenter presenter;
    HelpPresent helpPresent;

    //上传的附件
    ArrayList<MailFileDataBean> mailFileDataBeans = new ArrayList<MailFileDataBean>();
    //帮助文件
    ArrayList<MailFileDataBean> helpFileDataBeans = new ArrayList<MailFileDataBean>();
    MailFileAdapter fileAdapter;
    MailFileAdapter mailFileAdapter;

    //输入的模板内容
    ArrayList<HelpFileData> dtlList = new ArrayList<>();
    //配置详情列表
    //选中的模板
    HelpFileEntity helpFileEntity;

    //类型总类
    ArrayList<TypeData> datasEntities = new ArrayList<TypeData>();

    //选中的类型id
    private int ThingTypeId = 0;
    //选中的类型名称
    private String ThingTypeName = "";
    //选中的类型exportType
    private int exportType = 0;

    SubmitEntity entity;
    //1 部门  2 个人 3 群组
    private int choose = 2;

    String people = "";
    String department = "";
    String group = "";
    String copyPeople = "";

    private LoadingDialog loadingDialog;

    //附件原路径
    private String img_path;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 10000:
                usersBeans_origin = (ArrayList<People>) data.getExtras().getSerializable("persons");
                showSelectPerson();
                break;
            case 10001:
                departmentBeans = (ArrayList<ChildDepartmentsBean>) data.getExtras().getSerializable("departmentBeans");
                showSelectDepartment();
                break;
            case 10002:
                groupGroupData = (ArrayList<GroupGroupData>) data.getExtras().getSerializable("groups");
                showSelectGroup();
                break;
            case 20000:
                usersBeans_origin_copy = (ArrayList<People>) data.getExtras().getSerializable("persons");
                showSelectCopyPerson();
                break;
            case Activity.RESULT_OK:
                Uri uri = data.getData();//得到uri，后面就是将uri转化成file的过程。
                img_path = GetPathFromUri4kitkat.getPath(this, uri);
                presenter_upload.uploadFile(img_path);
                break;
            default:
                break;
        }
    }

    @Override
    public void setRootView() {
        setContentView(R.layout.act_thing_add);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        uploadDialog = new UploadDialog(this);
        presenter_upload = new UploadPresenter(new UploadModel(), this);
        presenter = new ThingAddPresenter(new ThingModel(), this);
        helpPresent = new HelpPresent(new HelpModel(), this);

        loadingDialog = new LoadingDialog(this);
        loadingDialog.setCanceledOnTouchOutside(false);

        getTypeData();

//        copeMeLl.setVisibility(View.VISIBLE);
        themeLl01.setVisibility(View.GONE);
        themeLl02.setVisibility(View.VISIBLE);
    }

    @Override
    @OnClick({R.id.back, R.id.bar_text_submit, R.id.et_person, R.id.thing_iv_person_choose, R.id.repair_ll_type,
            R.id.ll_extra_file, R.id.repair_help, R.id.select_depart, R.id.copy_me_et_person,
            R.id.copy_me_thing_iv_person_choose, R.id.btn_custom})
    public void onClick(View view) {
        if (CommonUtil.isFastClick()) {
            return;
        } else {
            switch (view.getId()) {
                case R.id.back:
                    finish();
                    break;
                case R.id.bar_text_submit:
                    if (thingTvType.getText().toString().isEmpty()) {
                        ViewInject.toast("类型不能为空");
                        break;
                    }
                    if (inputEtTitleThing02.getText().toString().isEmpty() &&
                            inputEtTitleThing03.getText().toString().isEmpty() &&
                            inputEtTitleThing04.getText().toString().isEmpty()) {
                        ViewInject.toast("主题不能为空");
                        break;
                    }
                    if (choose == 1 && departmentBeans.size() != 0) {
                        JudgeAnnualLeave();
                        break;
                    }
                    if (choose == 2 && usersBeans_origin.size() != 0) {
                        JudgeAnnualLeave();
                        break;
                    }
                    if (choose == 3 && groupGroupData.size() != 0) {
                        JudgeAnnualLeave();
                        break;
                    }
                    ViewInject.toast("人员不能为空");
                    break;
                case R.id.et_person:
                    turnActivity();
                    break;
                case R.id.thing_iv_person_choose:
                    turnActivity();
                    break;
                case R.id.copy_me_et_person:
                    selectCopy();
                    break;
                case R.id.copy_me_thing_iv_person_choose:
                    selectCopy();
                    break;
                case R.id.repair_ll_type:
                    showTypeDialog();
                    break;
                case R.id.ll_extra_file:
                    select_file();
                    break;
                case R.id.repair_help:
                    if (thingTvType.getText().toString().isEmpty()) {
                        new ToastDialog(aty, "请先选择类型").show();
                    } else {
                        helpFileDataBeans.clear();
                        helpFileShow();
                    }
                    break;
                case R.id.select_depart:
                    new ActionSheetDialog(aty)
                            .builder()
                            .setTitle("流转类型")
                            .setCancelable(false)
                            .setCanceledOnTouchOutside(false)
                            .addSheetItem("个人处理", ActionSheetDialog.SheetItemColor.Blue,
                                    new ActionSheetDialog.OnSheetItemClickListener() {
                                        @Override
                                        public void onClick(int which) {
                                            selectTxt.setText("个人处理");
                                            departorpeople.setText("个人处理");
                                            choose = 2;

                                            showSelectPerson();
                                        }
                                    })
                            .addSheetItem("部门处理", ActionSheetDialog.SheetItemColor.Blue,
                                    new ActionSheetDialog.OnSheetItemClickListener() {
                                        @Override
                                        public void onClick(int which) {
                                            selectTxt.setText("部门处理");
                                            departorpeople.setText("部门处理");
                                            choose = 1;
//                                        setGridView(departDatas);
                                            showSelectDepartment();
                                        }
                                    })
                            .addSheetItem("群组处理", ActionSheetDialog.SheetItemColor.Blue,
                                    new ActionSheetDialog.OnSheetItemClickListener() {
                                        @Override
                                        public void onClick(int which) {
                                            selectTxt.setText("群组处理");
                                            departorpeople.setText("群组处理");
                                            choose = 3;

                                            showSelectPerson();
                                        }
                                    }).show();
                    break;
                case R.id.btn_custom:
                    themeLl03.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }

    /**
     * 获取类型
     */
    private void getTypeData() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        presenter.getTypeDatas(params);
    }

    /**
     * 获取类型的模板
     */
    private void initPresenter() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("configId", ThingTypeId + "");
        presenter.getHelpFile(params);

        modelView.setVisibility(View.VISIBLE);
        if (!animProgress.isShown()) {
            animProgress.setVisibility(View.VISIBLE);
        }

        Map<String, String> params2 = new HashMap<>();
        params2.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params2.put("configId", ThingTypeId + "");
        params2.put("configType", "1");
        params2.put("level", "0");
        params2.put("fromMemberId", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.ACCOUNT_ID));
        presenter.getFlowMember(params2);
    }

    /**
     * 获取类型的帮助文件
     *
     * @param fileUuid
     */
    private void initFileData(String fileUuid) {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("fileUuid", fileUuid);
        helpPresent.getFileData(params);
    }

    @Override
    public void showTypeDatas(BaseEntity<TypeData> typeBean) {
        if (typeBean.getStatus().equals("200")) {
            datasEntities = typeBean.getDatas();
            TypeData datasEntity = new TypeData();
            datasEntity.setConfigId(-999);
            datasEntity.setConfigName("其他");
            datasEntities.add(datasEntity);
        } else {
            ViewInject.toast("获取类型失败");
        }
    }

    @Override
    public void uploadDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                uploadDialog.setCanceledOnTouchOutside(false);
                uploadDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                uploadDialog.dismiss();
                break;
        }
    }

    @Override
    public void updateFile(UploadBean uploadBean) {
        flvAttachment.setVisibility(View.VISIBLE);
        if (mailFileDataBeans.size() == 0) {
            uploadBean.getData().get(0).setFileUrl(img_path);
            mailFileDataBeans.add(uploadBean.getData().get(0));
            fileAdapter = new MailFileAdapter(this, mailFileDataBeans, true);
            flvAttachment.setAdapter(fileAdapter);
        } else {
            uploadBean.getData().get(0).setFileUrl(img_path);
            mailFileDataBeans.add(uploadBean.getData().get(0));
            fileAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getHelpFile(HelpFileBean helpFileBean) {
        if (animProgress.isShown()) {
            animProgress.setVisibility(View.GONE);
        }
        if (helpFileBean.getStatus().equals("200")) {
            if (helpFileBean.getDatas().size() > 0) {
                helpFileEntity = helpFileBean.getDatas().get(0);
                dtlList.clear();
                //类型显示模板
//                new ModelAddView(aty, dtlList).showView(helpFileEntity, modelView);
                new ModelAddViewNew(aty, dtlList).showView(helpFileEntity, modelView);
            } else {
                modelView.setVisibility(View.GONE);
            }

        } else {
            ViewInject.toast("获取类型模板失败");
        }
    }

    @Override
    public void getFlowMember(BaseEntity<FlowMemberData> s) {
        if (s.getStatus().equals("200")) {
            List<FlowMemberData> memberDatas = s.getDatas();
            if (memberDatas.size() > 0) {
                personChoose.setVisibility(View.GONE);
                selectDepart.setEnabled(false);
                departmentBeans.clear();
                usersBeans_origin.clear();

                String dep = "";

                if (memberDatas.get(0).getToMemberType() == 2) {
                    selectTxt.setText("部门处理");
                    departorpeople.setText("部门处理");
                    choose = 1;
                    for (FlowMemberData flowMemberData : memberDatas) {
                        ChildDepartmentsBean people = new ChildDepartmentsBean();
                        people.setDepartment_id(flowMemberData.getMemberId());
                        people.setDepartment_name(flowMemberData.getMemberName());
                        departmentBeans.add(people);
                        if (dep.isEmpty()) {
                            dep = flowMemberData.getMemberName();
                        } else {
                            dep = dep + ";" + flowMemberData.getMemberName();
                        }
                    }
                } else if (memberDatas.get(0).getToMemberType() == 1) {
                    selectTxt.setText("个人处理");
                    departorpeople.setText("个人处理");
                    choose = 2;
                    for (FlowMemberData flowMemberData : memberDatas) {
                        People people = new People();
                        people.setUserId(flowMemberData.getMemberId() + "");
                        people.setUserName(flowMemberData.getMemberName());
                        usersBeans_origin.add(people);
                        if (dep.isEmpty()) {
                            dep = flowMemberData.getMemberName();
                        } else {
                            dep = dep + ";" + flowMemberData.getMemberName();
                        }
                    }
                } else if (memberDatas.get(0).getToMemberType() == 3) {
                    selectTxt.setText("群组处理");
                    departorpeople.setText("群组处理");
                    choose = 3;
                    for (FlowMemberData flowMemberData : memberDatas) {
                        GroupGroupData groupGroupData1 = new GroupGroupData();
                        groupGroupData1.setGroupId(flowMemberData.getMemberId() + "");
                        groupGroupData1.setTitle(flowMemberData.getMemberName());
                        groupGroupData.add(groupGroupData1);
                        if (dep.isEmpty()) {
                            dep = flowMemberData.getMemberName();
                        } else {
                            dep = dep + ";" + flowMemberData.getMemberName();
                        }
                    }
                }

                etPerson.setText(dep);
                if (dep.isEmpty()) {
                    etPerson.setEnabled(true);
                } else {
                    etPerson.setEnabled(false);
                }
            } else {
                selectDepart.setEnabled(true);
                personChoose.setVisibility(View.VISIBLE);
                etPerson.setText("");
                etPerson.setEnabled(true);
            }
        }
    }

    @Override
    public void getAnnualLeave(BaseEntity<AnnualLeaveBean> s) {
        if (s.getStatus().equals("200")) {
            ApiForKjHttp();
        } else {
            ViewInject.toast(s.getMessage());
        }
    }

    @Override
    public void getFileData(MailFileBean mailFileBean) {
        if (mailFileBean.getStatus().equals("200")) {
            helpFileDataBeans.add(mailFileBean.getDatas().get(0));
            if (mailFileAdapter == null) {
                mailFileAdapter = new MailFileAdapter(this, helpFileDataBeans, false);
                helpAttachment.setAdapter(mailFileAdapter);
            } else {
                mailFileAdapter.notifyDataSetChanged();
            }
        } else {
            Log.e("jx", "文件信息获取失败");
        }
    }


    /**
     * 判断是否是年休假
     */
    private void JudgeAnnualLeave() {
        if (exportType == 2) {
            for (int i = 0; i < dtlList.size(); i++) {
                if (dtlList.get(i).getDtlName().equals("假期类别")) {
                    for (int j = 0; j < dtlList.get(i).getList().size(); j++) {
                        if (dtlList.get(i).getList().get(j).getDtlName().equals("年休假")) {
                            String dates = "";
                            for (HelpFileData fileData : dtlList) {
                                if (fileData.getDtlName().equals("请假时段")) {
                                    dates = fileData.getDtlValue();
                                }
                            }
                            getAnnualLeave(dates.split(" -- ")[0], dates.split(" -- ")[1]);
                            return;
                        }
                    }
                }
            }
            ApiForKjHttp();
        } else {
            ApiForKjHttp();
        }
    }

    /**
     * 获取年休假
     */
    private void getAnnualLeave(String startDate, String endDate) {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("configId", ThingTypeId + "");
        presenter.getAnnualLeave(params);
    }

    /**
     * 新建事项
     */
    private void ApiForKjHttp() {
        String BaseUrl = AppConfig.WEB_SYSTEM + "createTask.json?";
        String partUrl = "";
        for (int i = 0; i < mailFileDataBeans.size(); i++) {
            partUrl = partUrl + "attachmentId=" + mailFileDataBeans.get(i).getFileUuid() + "&";
        }
        if (partUrl != null && partUrl.length() > 0) {
            partUrl = partUrl.substring(0, partUrl.length() - 1);
        }
        BaseUrl = BaseUrl + partUrl;

        KJHttp client = new KJHttp();
        HttpParams params = new HttpParams();
        String copyName = "", copyId = "";
        for (int i = 0; i < usersBeans_origin_copy.size(); i++) {
            if (copyName.length() == 0) {
                copyName = usersBeans_origin_copy.get(i).getUserName();
            } else {
                copyName = copyName + ";" + usersBeans_origin_copy.get(i).getUserName();
            }
            if (copyId.length() == 0) {
                copyId = usersBeans_origin_copy.get(i).getUserId();
            } else {
                copyId = copyId + ";" + usersBeans_origin_copy.get(i).getUserId();
            }
        }
        if (choose == 1) {
            String id = departmentBeans.get(0).getDepartment_id() + "";
            String name = departmentBeans.get(0).getDepartment_name();
            params.put("toId", id);
            params.put("toName", name);
            params.put("toType", "2");
            params.put("carbonNames", copyName);
            params.put("carbonIds", copyId);
        } else if (choose == 2) {
            String id = usersBeans_origin.get(0).getUserId();
            String name = usersBeans_origin.get(0).getUserName();
            params.put("toId", id);
            params.put("toName", name);
            params.put("toType", "1");
            params.put("carbonNames", copyName);
            params.put("carbonIds", copyId);
        } else if (choose == 3) {
            String id = groupGroupData.get(0).getGroupId();
            String name = groupGroupData.get(0).getTitle();
            params.put("toId", id);
            params.put("toName", name);
            params.put("toType", "3");
            params.put("carbonNames", copyName);
            params.put("carbonIds", copyId);
        }

        params.put("content", inputEtContentThing.getText().toString());

        //主题
        if (inputEtTitleThing02.getText().toString().length() == 0) {
            params.put("title", inputEtTitleThing04.getText().toString());
        } else {
            if (inputEtTitleThing03.getText().toString().length() > 0) {
                params.put("title", inputEtTitleThing03.getText().toString());
            } else {
                params.put("title", inputEtTitleThing02.getText().toString());
            }
        }

        params.put("state", "1");
        params.put("configId", ThingTypeId);
        params.put("configName", ThingTypeName);

        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        if (dtlList != null && !new ModelAddViewNew(aty, dtlList).isEmpty()) {
            loadingDialog.show();
            params.put("dtlJson", JSONArray.toJSONString(dtlList));
        } else {
            return;
        }
//        if (dtlList != null && !new ModelAddView(aty, dtlList).isEmpty()) {
//            params.put("dtlJson", JSONArray.toJSONString(dtlList));
//        }else{
//            return;
//        }

        client.post(BaseUrl, params, new HttpCallBack() {
            @Override
            public void onSuccess(String t) {
                super.onSuccess(t);
                entity = JSON.parseObject(t, SubmitEntity.class);
                setUi();
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                ViewInject.toast(aty.getString(R.string.create_fail));
                loadingDialog.dismiss();
            }
        });
    }

    private void setUi() {
        if (entity.getStatus().equals("200")) {
            ViewInject.toast(aty.getString(R.string.create_success));
            loadingDialog.dismiss();
            finish();
        } else {
            ViewInject.toast(aty.getString(R.string.create_fail));
            loadingDialog.dismiss();
        }
    }

    /**
     * 获取类型的帮助文件
     */
    private void helpFileShow() {
        if (helpFileEntity != null && helpFileEntity.getConfigUrls() != null &&
                !helpFileEntity.getConfigUrls().isEmpty()) {
            //帮助文件URL用;隔开
            String temp[] = helpFileEntity.getConfigUrls().split(";");
            for (int i = 0; i < temp.length; i++) {
                if (!temp[i].isEmpty()) {
                    initFileData(temp[i]);
                }
            }
            helpAttachment.setVisibility(View.VISIBLE);
        } else {
            ViewInject.toast("没有帮助文件");
            helpAttachment.setVisibility(View.GONE);
        }
    }

    /**
     * 选择类型dialog
     */
    private void showTypeDialog() {
        ActionSheetDialog actionSheetDialog;
        actionSheetDialog = new ActionSheetDialog(this).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true);

        for (int i = 0; i < datasEntities.size(); i++) {
            final int final1 = i;
            actionSheetDialog.addSheetItem(datasEntities.get(i).getConfigName(), ActionSheetDialog.SheetItemColor.Blue,
                    new ActionSheetDialog.OnSheetItemClickListener() {
                        @Override
                        public void onClick(int which) {

                            if (datasEntities.get(final1).getConfigTopic() != null && datasEntities.get(final1).getConfigTopic().length() > 0) {
                                themeLl02.setVisibility(View.VISIBLE);
                                themeLl03.setVisibility(View.GONE);
                                themeLl04.setVisibility(View.GONE);
                                inputEtTitleThing02.setText(datasEntities.get(final1).getConfigTopic());
                                inputEtTitleThing03.setText("");
                                inputEtTitleThing04.setText("");
                            } else {
                                themeLl02.setVisibility(View.GONE);
                                themeLl03.setVisibility(View.GONE);
                                themeLl04.setVisibility(View.VISIBLE);
                                inputEtTitleThing02.setText("");
                                inputEtTitleThing03.setText("");
                                inputEtTitleThing04.setText("");
                            }


                            thingTvType.setText(datasEntities.get(final1).getConfigName());
                            ThingTypeId = datasEntities.get(final1).getConfigId();
                            ThingTypeName = datasEntities.get(final1).getConfigName();
                            exportType = datasEntities.get(final1).getExportType();
                            helpAttachment.setVisibility(View.GONE);

                            modelView.removeAllViews();
                            if (datasEntities.get(final1).getConfigId() != -999) {
                                initPresenter();
                            } else {
                                if (dtlList.size() > 0) {
                                    dtlList.clear();
                                }
                                selectDepart.setEnabled(true);
                                personChoose.setVisibility(View.VISIBLE);
                                modelView.setVisibility(View.GONE);
                                etPerson.setText("");
                            }
                        }
                    });
        }
        actionSheetDialog.show();
    }

    /**
     * 从设备读取文件
     */
    private void select_file() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");//设置类型，我这里是任意类型，任意后缀的可以这样写。
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, 1);
    }

    /**
     * 显示选中的人
     */
    private void showSelectPerson() {
        people = "";
        for (People childDepartmentsBean : usersBeans_origin) {
            if (people.isEmpty()) {
                people = childDepartmentsBean.getUserName();
            } else {
                people = people + ";" + childDepartmentsBean.getUserName();
            }
        }
        etPerson.setText(people);
    }

    /**
     * 显示选中的部门
     */
    private void showSelectDepartment() {
        department = "";
        for (ChildDepartmentsBean childDepartmentsBean : departmentBeans) {
            if (department.isEmpty()) {
                department = childDepartmentsBean.getDepartment_name();
            } else {
                department = department + ";" + childDepartmentsBean.getDepartment_name();
            }
        }
        etPerson.setText(department);
    }

    /**
     * 显示选中的群组
     */
    private void showSelectGroup() {
        group = "";
        for (GroupGroupData groupGroupData : groupGroupData) {
            if (group.isEmpty()) {
                group = groupGroupData.getTitle();
            } else {
                group = group + ";" + groupGroupData.getTitle();
            }
        }
        etPerson.setText(group);
    }

    /**
     * 显示选中的抄送人
     */
    private void showSelectCopyPerson() {
        copyPeople = "";
        for (People childDepartmentsBean : usersBeans_origin_copy) {
            if (copyPeople.isEmpty()) {
                copyPeople = childDepartmentsBean.getUserName();
            } else {
                copyPeople = copyPeople + ";" + childDepartmentsBean.getUserName();
            }
        }
        copyMeEtPerson.setText(copyPeople);
    }

    /**
     * 跳转界面，选择对象
     */
    private void turnActivity() {
        if (choose == 1) {
            Bundle bundle = new Bundle();
            Intent intent = new Intent(this, Act_Department.class);
            bundle.putSerializable("departmentBeans", departmentBeans);
            bundle.putInt("fromWhere", 2);
            intent.putExtras(bundle);
            aty.startActivityForResult(intent, 1);
        } else if (choose == 2) {
            Bundle bundle = new Bundle();
            Intent intent = new Intent(this, Act_Choice_Member.class);
            bundle.putSerializable("persons", usersBeans_origin);
            bundle.putString("title", "新建事项");
            bundle.putString("type", "单选");
            intent.putExtras(bundle);
            aty.startActivityForResult(intent, 1);
        } else if (choose == 3) {
            Bundle bundle = new Bundle();
            Intent intent = new Intent(this, Act_Group.class);
            bundle.putSerializable("group", groupGroupData);
            bundle.putInt("isSelectMe", 1);
            bundle.putInt("type", 0);
            intent.putExtras(bundle);
            aty.startActivityForResult(intent, 1);
        }
    }

    private void selectCopy() {
        Bundle bundle = new Bundle();
        Intent intent = new Intent(this, Act_Choice_Member.class);
        bundle.putSerializable("persons", usersBeans_origin_copy);
        bundle.putString("title", "新建事项");
        bundle.putString("type", "多选");
        bundle.putString("iscopy", "2");
        intent.putExtras(bundle);
        aty.startActivityForResult(intent, 1);
    }

}
